一步之遥

题目 一步之遥

image-831e5995

思路分析

image-c9b84c7d

代码实现

#include<bits/stdc++.h>
using namespace std;
#define endl '\n'

const int N=1e6;
int d[N];

bool isVaild(int x){
	return x>=-N && x<=N && d[x]==-1;
}

int bfs(int u){
	queue<int> q;
	memset(d,-1,sizeof d);
	q.push(u);
	d[u]=0;

	while(!q.empty()){
		auto cur=q.front();q.pop();

		if(cur==1){
			return d[cur];
		}
		int choice1=cur+97,choice2=cur-127;
		if(isVaild(choice1)){
			q.push(choice1);
			d[choice1]=d[cur]+1;
		}
		if(isVaild(choice2)){
			q.push(choice2);
			d[choice2]=d[cur]+1;
		}
	}
}

int main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cout<<bfs(0);
	return 0;
}

同类题型

视频讲解


⬅️ 第七届蓝桥杯大赛软件赛决赛C/C++ 大学 B 组 🏠 00-冲刺国赛 ➡️ 凑平方数